<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 列表卡片区域 -->
    <el-card>
      <!-- 搜索框区域 -->
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入订单号" v-model="queryInfo.query" clearable>
            <el-button slot="append" icon="el-icon-search" @click="queryOrderList"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单数据列表区域 -->
      <el-table :data="orderList" border stripe>
        <!-- 索引列 -->
        <el-table-column type="index"></el-table-column>
        <!-- 订单号列 -->
        <el-table-column label="订单号" prop="order_number"></el-table-column>
        <!-- 订单金额列 -->
        <el-table-column label="订单金额" prop="order_price"></el-table-column>
        <!-- 订单状态列 -->
        <el-table-column label="订单状态">
          <template v-slot="scoped">
            <el-tag type="success" v-if="scoped.row.pay_status === '1'">已付款</el-tag>
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <!-- 发货状态列 -->
        <el-table-column label="已发货" prop="is_send"></el-table-column>
        <!-- 创建时间列 -->
        <el-table-column label="创建时间" prop="create_time">
          <template v-slot="scoped">{{scoped.row.create_time | dateFormat}}</template>
        </el-table-column>
        <!-- 操作列 -->
        <el-table-column label="操作">
          <template v-slot="scoped">
            <!-- 修改订单地址按钮 -->
            <el-tooltip
              class="item"
              effect="dark"
              content="修改订单地址"
              placement="top"
              :enterable="false"
            >
              <el-button
                size="small"
                type="primary"
                icon="el-icon-edit"
                circle
                @click="showAddressDialog"
              ></el-button>
            </el-tooltip>
            <!-- 修改订单状态按钮 -->
            <el-tooltip
              class="item"
              effect="dark"
              content="修改订单状态"
              placement="top"
              :enterable="false"
            >
              <el-button
                size="small"
                type="warning"
                icon="el-icon-s-order"
                circle
                @click="showOrderDetailDialog(scoped.row.order_id)"
              ></el-button>
            </el-tooltip>
            <!-- 查看物流按钮 -->
            <el-tooltip
              class="item"
              effect="dark"
              content="查看物流"
              placement="top"
              :enterable="false"
            >
              <el-button
                size="small"
                type="success"
                icon="el-icon-truck"
                circle
                @click="showProgressDialog"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 修改订单地址对话框 仅做省市区联动的选择器效果，实际不发起请求更改地址-->
    <el-dialog
      title="修改订单地址"
      :visible.sync="addressDialogVisible"
      width="50%"
      @close="addressDialogClosed"
    >
      <!-- 修改订单地址表单 -->
      <el-form
        :model="addressChangeForm"
        :rules="addressChangeFormRules"
        ref="addressChangeFormRef"
        label-width="100px"
      >
        <!-- 省市区县级联选择器 -->
        <el-form-item label="省市/区/县" prop="address1">
          <el-cascader
            v-model="addressChangeForm.address1"
            :options="cityData"
            :props="{expandTrigger:'hover'}"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
        <!-- 详细地址 -->
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressChangeForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addressDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改订单状态对话框 -->
    <el-dialog
      title="修改订单状态"
      :visible.sync="orderDetaildialogVisible"
      width="50%"
      @close="orderStatusDialogClosed"
    >
      <!-- 订单详情表单区域 -->
      <el-form
        :model="orderDetailForm"
        ref="orderDetailFormRef"
        label-width="100px"
        class="demo-ruleForm"
        label-position="top"
      >
        <el-form-item label="订单号">
          <el-input disabled v-model="orderDetailForm.order_number"></el-input>
        </el-form-item>
        <el-form-item label="创建时间">
          <el-input disabled :value="orderDetailForm.create_time | dateFormat"></el-input>
        </el-form-item>
        <!-- 是否付款 -->
        <el-form-item label="是否付款">
          <el-switch
            style="display: block"
            v-model="orderDetailForm.pay_status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="已付款"
            inactive-text="未付款"
            active-value="1"
            inactive-value="0"
          ></el-switch>
        </el-form-item>
        <!-- 付款方式 -->
        <el-form-item label="付款方式">
          <el-radio-group v-model="orderDetailForm.order_pay" @change="orderPayChange">
            <el-radio border :label="0">未支付</el-radio>
            <el-radio border :label="1">支付宝</el-radio>
            <el-radio border :label="2">微信</el-radio>
            <el-radio border :label="3">银行卡</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- 是否发货 -->
        <el-form-item label="是否发货">
          <el-switch
            style="display: block"
            v-model="orderDetailForm.is_send"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="已发货"
            inactive-text="未发货"
            active-value="是"
            inactive-value="否"
          ></el-switch>
        </el-form-item>
      </el-form>
      <!-- 按钮区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="orderDetaildialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="changeOrderStatus">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 物流进度信息对话框 -->
    <el-dialog title="物流信息" :visible.sync="progressDialogVisible" width="50%">
      <!-- 物流时间线 -->
      <el-timeline reverse="reverse">
        <el-timeline-item
          v-for="(activity, index) in progressInfo"
          :key="index"
          :timestamp="activity.time"
          placement="top"
          type="success"
        >{{activity.context}}</el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>
<script>
// 导入中国省市区县数据文件
import cityData from './citydata';

export default {
  data() {
    return {
      // 订单列表请求参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      //   订单列表
      orderList: [],
      //   订单总数
      total: 0,
      //   省市区县的数据
      cityData,
      //   控制修改订单信息对话框显示与隐藏
      addressDialogVisible: false,
      //   修改订单地址表单
      addressChangeForm: {
        //   省市区县选择
        address1: [],
        //   详细地址
        address2: ''
      },
      //   修改订单信息表单校验规则
      addressChangeFormRules: {
        address1: [
          { required: true, message: '请选择省市/区/县', trigger: 'change' }
        ],
        address2: [
          { required: true, message: '请填写详细地址', trigger: 'blur' }
        ]
      },
      //   控制物流信息对话框显示与隐藏
      progressDialogVisible: false,
      //   物流信息假数据
      progressInfo: [],
      // 控制订单详情对话框显示与隐藏
      orderDetaildialogVisible: false,
      // 订单详情表单对象
      orderDetailForm: {}
    };
  },
  created() {
    this.getOrderList();
  },
  methods: {
    //   获取订单列表数据
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo
      });
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.status + ' : 获取订单列表失败！');
      }
      //   console.log(res);
      //   保存总数
      this.total = res.data.total;
      //   保存订单列表数据
      this.orderList = res.data.goods;
    },
    // 搜索框按钮点击事件(并没有什么用，接口似乎没有处理query参数)
    async queryOrderList() {
      this.queryInfo.pagenum = 1;
      this.getOrderList();
    },
    // 每页显示几条数据
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getOrderList();
    },
    // 当前页变化
    handleCurrentChange(page) {
      this.queryInfo.pagenum = page;
      this.getOrderList();
    },
    // 显示修改订单信息对话框
    showAddressDialog() {
      this.addressDialogVisible = true;
    },
    // 省市区县级联选择器变化
    handleChange() {
      console.log(this.addressChangeForm.address1);
    },
    // 修改订单地址对话框关闭
    addressDialogClosed() {
      this.$refs.addressChangeFormRef.resetFields();
    },
    // 显示物流信息对话框
    async showProgressDialog() {
      // 获取物流信息请求，信息仅为测试数据，非真实
      const { data: res } = await this.$http.get('/kuaidi');
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.status + ' : 获取物流信息失败！');
      }
      //   console.log(res);
      this.progressInfo = res.data;
      this.progressDialogVisible = true;
      //   console.log(this.progressInfo);
    },
    // 显示修改订单状态对话框
    async showOrderDetailDialog(id) {
      // console.log(id);
      const { data: res } = await this.$http.get('orders/' + id);
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.status + ' : 获取订单详情失败！');
      }
      // console.log(res);
      // 转换为数字型
      res.data.order_pay = res.data.order_pay * 1;
      this.orderDetailForm = res.data;
      // console.log(this.orderDetailForm);
      this.orderDetaildialogVisible = true;
    },
    // 修改订单状态对话框关闭
    orderStatusDialogClosed() {
      this.$refs.orderDetailFormRef.resetFields();
    },
    // 单选框组选项变化事件
    orderPayChange(val) {
      console.log(val);
    },
    // 修改订单状态提交按钮
    async changeOrderStatus() {
      // console.log(this.orderDetailForm);
      const changeParams = {};
      changeParams.is_send = this.orderDetailForm.is_send === '是' ? 1 : 0;
      // 无论pay_status是字符串还是数字型提交，后台返回的永远是字符串0
      changeParams.pay_status = this.orderDetailForm.pay_status;
      changeParams.order_pay = this.orderDetailForm.order_pay + '';
      // 接口返回提示订单价格不能为空
      changeParams.order_price = this.orderDetailForm.order_price;
      // console.log(changeParams);
      const { data: res } = await this.$http.put(
        'orders/' + this.orderDetailForm.order_id,
        changeParams
      );
      if (res.meta.status !== 201) {
        return this.$message.error(res.meta.status + ' : 修改订单状态失败！');
      }
      this.$message.success('修改订单状态成功！');
      this.queryInfo.pagenum = 1;
      this.getOrderList();
      this.orderDetaildialogVisible = false;
      // console.log(res.data);
    }
  }
};
</script>
<style lang="less" scoped>
// 引入相关样式文件
@import '../../plugins/timeline/timeline.css';
@import '../../plugins/timeline-item/timeline-item.css';

.el-cascader {
  width: 100%;
}
</style>
